In 1912, the ship RMS Titanic struck an iceberg on its maiden voyage and sank, resulting in the deaths of most of its passengers and crew. In this project, we will explore the RMS Titanic passenger manifest to determine whether someone survived or did not survive.Demographics and passenger information from 891 of the 2224 passengers and crew on board the Titanic Dataset is obtained from kaggle (https://www.kaggle.com/c/titanic/data).
In [2]:
import numpy as np
import pandas as pd
from IPython.display import display
%matplotlib inline
# Load the dataset
files = "titanic_data.csv"
data_titanic = pd.read_csv(files)
display(data_titanic.head())
From a sample of the RMS Titanic data, we can see the various features present for each passenger on the ship:
NaN
)NaN
)Variable Notes
pclass: A proxy for socio-economic status (SES)
age: Age is fractional if less than 1. If the age is estimated, is it in the form of xx.5
sibsp: The dataset defines family relations in this way... Sibling = brother, sister, stepbrother, stepsister Spouse = husband, wife (mistresses and fiancés were ignored)
parch: The dataset defines family relations in this way... Parent = mother, father Child = daughter, son, stepdaughter, stepson Some children travelled only with a nanny, therefore parch=0 for them.
In [2]:
data =data_titanic
# Show the dataset
display(data.head())
data.info()
From the above info(),We can see columns Age, Cabin and Embarked have missing values.
Handling the missing values:
Ignore the rows with missing data,
Exclude the variable at all or we might substite it with mean or median.
Age 80% of the data is available,which seems a important variable so not to exclude.
Port of embarkation doesn't seem interesting.
cabin 23% of the data so decided to exclude.
PassengerId,Name,fare doesnt seem to contribute to any survival investigation
In [3]:
#exculding some coloumns
for a in ['Ticket','Cabin','Embarked','Name','PassengerId','Fare']:
if a in data.columns:
del data[a]
In [4]:
print "Age median values by Age and Sex:"
#we are grouping by gender and class and taking median of age so we can replace with corrresponding values instead of NaN
print data.groupby(['Sex','Pclass'], as_index=False).median().loc[:, ['Sex','Pclass', 'Age']]
print "Age values for 5 first persons in dataset:"
print data.loc[data['Age'].isnull(),['Age','Sex','Pclass']].head(5)
# apply transformation: Age missing values are filled with regard to Pclass and Sex:
data.loc[:, 'Age'] = data.groupby(['Sex','Pclass']).transform(lambda x: x.fillna(x.median()))
print data.loc[[5,17,19,26,28],['Age','Sex','Pclass']].head(5)
data['Age'] = data['Age'].fillna(data['Age'].mean())
In [6]:
data_s=data
survival_group = data_s.groupby('Survived')
survival_group.describe()
Out[6]:
From the above statistics
In [7]:
import matplotlib.pyplot as plt
import seaborn as sns
# Set style for all graphs
#sns.set_style("light")
#sns.set_style("whitegrid")
sns.set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})
From the above plot we can see how female individuals are given 1st preference and based on class.
Social-economic standing was a factor in survival rate of passengers by gender
Class 1 - male survival rate: 36.89%
Class 2 - female survival rate: 92.11%
Class 2 - male survival rate: 15.74%
Class 3 - female survival rate: 50.0%
In [13]:
def group(d,v):
if (d == 'female') and (v >= 18):
return 'Woman'
elif v < 18:
return 'child'
elif (d == 'male') and (v >= 18):
return 'Man'
data['Category'] = data.apply(lambda row:group(row['Sex'], row['Age']), axis=1)
data.head(5)
Out[13]:
In [ ]:
# We are dividing the Age data into 3 buckets of (0-18),(18-40),(40-90)
# and labeling them as 'Childs','Adults','Seniors' respectively
data['group_age'] = pd.cut(data['Age'], bins=[0,18,40,90], labels=['Childs','Adults','Seniors'])
#finding mean Survival rate grouped by 'group_age','Sex'.
df = data.groupby(['group_age','Sex',"Pclass"],as_index=False).mean().loc[:,['group_age','Sex',"Pclass",'Survived']]
df.to_csv("titanic_group_age.csv", sep=',', encoding='utf-8')
In [17]:
data_C=data.groupby(['Category',"Parch"]).mean()
data_C.sort("Survived")["Survived"]
data_C.to_csv("Category.csv", sep=',', encoding='utf-8')
In [31]:
data['Age_group'] = pd.cut(data['Age'], bins=range(0,90,10))
data_age=data.groupby(["Age_group"]).mean()
data_age.to_csv("Age_group.csv", sep=',', encoding='utf-8')
In [10]:
%run P2
From the above values we can see that the survival rate is increasing from top to bottom. And the from the plot we can see the distribution of survival rate among men ,women and children,based on class.
We observe a order of survival rate based on Age ,Sex and Class:
children and women of upper class |
---|
children and women of middle class |
women of lower class |
children of lower class |
men of upper class |
finally men of the middle class and lower class have least survival rate |
The analysis seems that , A female with upper social-economic standing (first class) and Children,had the best chance of survival. Age did not seem to be a major factor.Man in third class, had the lowest chance of survival. Women and children of all classes, were mostly having a higher survival rate than men in general.
Limitations: